/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.events.activity;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import org.openide.util.Lookup;
import pt.iasd.youthshelter.datamodel.Involvement;
import pt.iasd.youthshelter.datamodel.service.InvolvementController;
import pt.iasd.youthshelter.wizards.core.WizardKeys;

public final class EditActivityParticipationVisualPanel extends JPanel implements ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * Creates new form EditActivityParticipationVisualPanel
     */
    public EditActivityParticipationVisualPanel() {
        this(null);
    }
    
    /**
     * Creates new form EditActivityParticipationVisualPanel
     */
    public EditActivityParticipationVisualPanel(Involvement involv) {
        initComponents();
        if(involv != null) {
            this.InvolvementComboBox.setSelectedItem(involv);
            actionPerformed(null);
        }
    }

    @Override
    public String getName() {
        return WizardKeys.PROP_GOTOACTIVITY_STEPS[0];
    }

    public JComboBox getInvolvementComboBox() {
        return InvolvementComboBox;
    }

    public JLabel getSelectedActivityLabel() {
        return SelectedActivityLabel;
    }

    public JLabel getSelectedPathfinderLabel() {
        return SelectedPathfinderLabel;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PathfinderLabel = new javax.swing.JLabel();
        ActivityLabel = new javax.swing.JLabel();
        InvolvementLabel = new javax.swing.JLabel();
        InvolvementComboBox = new javax.swing.JComboBox();
        SelectedPathfinderLabel = new javax.swing.JLabel();
        SelectedActivityLabel = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(PathfinderLabel, bundle.getString("EditActivityParticipationVisualPanel.PathfinderLabel.text"));

        org.openide.awt.Mnemonics.setLocalizedText(ActivityLabel, bundle.getString("EditActivityParticipationVisualPanel.ActivityLabel.text"));

        org.openide.awt.Mnemonics.setLocalizedText(InvolvementLabel, bundle.getString("EditActivityParticipationVisualPanel.InvolvementLabel.text"));

        List<Involvement> involvements = loadInvolvementComboBoxData();
        InvolvementComboBox.setModel((involvements == null || involvements.isEmpty())? new DefaultComboBoxModel() : new DefaultComboBoxModel(involvements.toArray(new Involvement[involvements.size()])));
        if(involvements != null && !involvements.isEmpty()) {
            InvolvementComboBox.setSelectedIndex(0);
        }
        InvolvementComboBox.setPreferredSize(new java.awt.Dimension(150, 20));
        InvolvementComboBox.setRenderer(new InvolvementRenderer());
        InvolvementComboBox.addActionListener(this);

        SelectedPathfinderLabel.setPreferredSize(new java.awt.Dimension(300, 14));

        SelectedActivityLabel.setPreferredSize(new java.awt.Dimension(200, 14));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(InvolvementLabel)
                    .addComponent(ActivityLabel)
                    .addComponent(PathfinderLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(InvolvementComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SelectedPathfinderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SelectedActivityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PathfinderLabel)
                    .addComponent(SelectedPathfinderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ActivityLabel)
                    .addComponent(SelectedActivityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(InvolvementLabel)
                    .addComponent(InvolvementComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ActivityLabel;
    private javax.swing.JComboBox InvolvementComboBox;
    private javax.swing.JLabel InvolvementLabel;
    private javax.swing.JLabel PathfinderLabel;
    private javax.swing.JLabel SelectedActivityLabel;
    private javax.swing.JLabel SelectedPathfinderLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }

    // <editor-fold defaultstate="collapsed" desc="Inner Classes">
    private class InvolvementRenderer extends BasicComboBoxRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(value instanceof Involvement) {
                setText(((Involvement)value).getName());
            } else {
                setText("");
            }
            return this;
        }
    } // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="ComboBox Data Loading Code">
    private List loadInvolvementComboBoxData() {
        InvolvementController controller = Lookup.getDefault().lookup(InvolvementController.class);
        if(controller != null) {
            try{
                List list = controller.findInvolvementEntities();
                if(!list.isEmpty()) {
                    return list;
                }
            } catch(Exception ex) {
                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        return null;
    }// </editor-fold>
}
